package cn.cmvideo.springboot.algorithm;

/**
 * 数组元素奇数放在左边，偶数放在右边，返回第一个偶数，时间复杂度O(1),空间复杂度O(1)<br>
 * 解决方式：两个指针，一个指向头部，一个指向尾部
 * 
 * @author Administrator
 *
 */
public class OddEvenNumber
{
	public static int oddEven(int[] number)
	{
		int length = number.length;
		if (length <= 0)
		{
			return -1;
		}
		int front = 0;
		int end = length - 1;

		while (front < end)
		{
			while (front < length && (number[front] & 1) == 1)
			{
				front++; // 从头查找偶数
			}
			while (end >= 0 && (number[end] & 1) == 0)
			{
				end--; // 从尾部查找奇数
			}

			if (front < end) // 奇数和偶数交换
			{
				int temp = number[front];
				number[front] = number[end];
				number[end] = temp;
			}
		}
		return number[front];
	}

	public static void main(String[] args)
	{
		int[] number = new int[] { 1, 4, 3, 6, 5, 7, 8, 9 };
		int oddEven = oddEven(number);
		// for (int i = 0; i < oddEven.length; i++)
		// {
		// System.out.print(oddEven[i]);
		// }
		// System.out.print("\n");
		System.out.println(2 & 1);
	}

}
